import 'package:dotted_line/dotted_line.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:gw_github_flutter/common/global.dart';
import 'package:gw_github_flutter/common/gwNet.dart';
import 'package:gw_github_flutter/common/notifier.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/generated/l10n.dart';
import 'package:gw_github_flutter/models/index.dart';
import 'package:gw_github_flutter/route/home/homeDrawer.dart';
import 'package:provider/provider.dart';

class HomeRoute extends StatefulWidget {
  HomeRoute({Key? key}) : super(key: key);
  @override
  State<HomeRoute> createState() => _HomeRouteState();
}

class _HomeRouteState extends State<HomeRoute> {
  static const loadingTag = "##loading##"; //表尾标记
  // ignore: prefer_final_fields
  var _items = [
    Repo()
      ..name = RouteName.systemRoute + " button textField"
      ..route_path = RouteName.systemRoute,
    Repo()
      ..name = RouteName.alertOrDialogRoute
      ..route_path = RouteName.alertOrDialogRoute,
    Repo()
      ..name = RouteName.sliverRoute + " 滑动视图 tableview 排序"
      ..route_path = RouteName.sliverRoute,
    Repo()
      ..name = RouteName.wrapRoute + " 流式布局"
      ..route_path = RouteName.wrapRoute,
    Repo()
      ..name = RouteName.swiperRoute + " 轮播图"
      ..route_path = RouteName.swiperRoute,
    Repo()
      ..name = RouteName.toolsRoute + " 工具 "
      ..route_path = RouteName.toolsRoute,
    Repo()..name = loadingTag
  ];
  bool hasMore = true; //是否还有数据
  int page = 1; //当前请求的是第几页

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).home),
      ),
      body: _buildBody(),
      drawer: HomeDrawer(),
    );
  }

  Widget _buildBody() {
    UserModel userModel = Provider.of<UserModel>(context);
    return ListView.separated(
      itemCount: _items.length,
      itemBuilder: (context, index) {
        if (_items[index].name == loadingTag) {
          return Container(
            padding: const EdgeInsets.all(16.0),
            alignment: Alignment.center,
            child: Text(
              "没有更多了",
              style: TextStyle(color: Colors.grey),
            ),
          );
        }
        return ListTile(
          title: Text(
            _items[index].name,
            style: TextStyle(fontSize: 13),
          ),
          subtitle: Text(_items[index].route_path ?? ""),
          onTap: () {
            Navigator.of(context).pushNamed(_items[index].route_path ?? "");
          },
        );
      },
      separatorBuilder: (context, index) {
        return const DottedLine(dashColor: Colors.red,);
        // return Divider(
        //   height: 0,
        // );
      },
    );
  }

  void _reqData() async {
    var data = await GwNet(context).getRepos(
      queryParameters: {
        'page': page,
        'page_size': 20,
      },
    );

    //如果返回的数据小于指定的条数，则表示没有更多数据，反之则否
    hasMore = data.length > 0 && data.length % 20 == 0;

    setState(() {
      _items.insertAll(_items.length - 1, data);
      page++;
    });
  }
}
